package cn.com.edu.nju.ecrec.util;

import javax.persistence.*;
import javax.servlet.http.HttpServlet;

public class JPAFactory extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	private static EntityManagerFactory emf;

	public static synchronized EntityManager getManager() {
		if (emf == null) {
			emf = Persistence.createEntityManagerFactory("mbmaUnit");
		}
		
		return emf.createEntityManager();
	}

	public void init() {
		if (emf == null) {
			emf = Persistence.createEntityManagerFactory("mbmaUnit");
		}
	}

	public void destroy() {
		if (emf.isOpen()) {
			emf.close();
		}
	}
}